package com.jiazaibendi.lottery.service.impl;

import com.jiazaibendi.lottery.common.CommonConstant;
import com.jiazaibendi.lottery.service.PrizePoolService;
import com.jiazaibendi.lottery.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description
 *
 * @author echo_kai
 * @version 1.0
 * @date 2021/08/07
 */
@Service
@Slf4j
public class PrizePoolServiceImpl implements PrizePoolService {

    @Resource
    private RedisService redisService;

    @Override
    public void addCodeToEnvelopePool(long schemeId, long prizeId, List<String> code) {
        redisService.lPushAll(CommonConstant.ENVELOPE_POOL_PREFIX + ":" + schemeId + ":" + prizeId, CommonConstant.DAY_SECONDS, code.toArray());
    }

    @Override
    public String getACodeFromEnvelopePool(long schemeId, long prizeId) {
        return (String) redisService.lPop(CommonConstant.ENVELOPE_POOL_PREFIX + ":" + schemeId + ":" + prizeId);
    }

    @Override
    public long getPrizeIdFromPrizePool(long schemeId) {
        Integer prize = (Integer) redisService.lPop(CommonConstant.PRIZE_POOL_PREFIX + ":" + schemeId);
        if (prize == null) return 0;
        return prize.longValue();
    }

    @Override
    public long getPoolPrizeNumber(long schemeId) {
        return redisService.lSize(CommonConstant.PRIZE_POOL_PREFIX + ":" + schemeId);
    }

    @Override
    public void addPrizeToPrizePool(long schemeId, List<Long> prizeIds) {
        redisService.lPushAll(CommonConstant.PRIZE_POOL_PREFIX + ":" + schemeId, prizeIds.toArray());
    }

    @Override
    public List<Long> queryAllPrizeOfPrizePool(long schemeId) {
        return redisService.lRange(CommonConstant.PRIZE_POOL_PREFIX + ":" + schemeId, 0, -1).stream().map(o -> Long.valueOf(o.toString())).collect(Collectors.toList());
    }

    @Override
    public void clearPrizePool(long schemeId) {
        redisService.del(CommonConstant.PRIZE_POOL_PREFIX + ":" + schemeId);
    }
}
